<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

class Backend {
	public static function startup() {
		Auth::factory();
		if (!Auth::isLogged()) {	
			if (!Router::checkRoute('user', 'login')) {
				Tools::redirect(BASE_URL . 'user/login');
			}
		} else {
			if (!User::checkAccess(Auth::getId(), Router::getController(), Router::getAction())) {
				if (!Router::checkRoute('user', 'logout')) {
					die('Access denied');
				}
			}
		}

		$curr_lang = Config::get('schema_default', 'i18n');
		if (Session::is('i18n_schema_current')) {
			$session_lang = trim(strtolower(Session::get('i18n_schema_current')));
			if (in_array($session_lang, Config::get('schema_list', 'i18n'))) {
				$curr_lang = $session_lang;
			}
		}

		Config::set('schema_current', $curr_lang, 'i18n');
		Session::set('i18n_schema_current', $curr_lang);

		if (in_array($curr_lang, Config::get('schema_rtl_list', 'i18n'))) {
			Config::set('schema_rtl', true, 'i18n');
		}

		if (in_array(Router::getLanguage(), Config::get('rtl_list', 'i18n'))) {
			Config::set('rtl', true, 'i18n');
			Config::set('align', 'right', 'i18n');
			Config::set('align_not', 'left', 'i18n');
		}
	}
	
	public static function infoMessage($str) { ?>
		<div class="alert alert-success">
			<?php echo $str; ?>
		</div>
	<?php
	}

	public static function errorMessage($str) { ?>
		<div class="alert alert-error">
			<?php echo $str; ?>
		</div>
	<?php
	}
}
